﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Web.Mvc;
using ARNative;

namespace NRemedy.MVC.UI.Test
{
    [TestClass]
    public class TableDataController_Test
    {
        [TestMethod]
        public void TableRequest_Parse_Test1()
        {
            TableController controller = new TableController();
            FormCollection collection = new FormCollection();
            collection.Add("fields", "536870923|536870924");
            collection.Add("rows", "10");
            collection.Add("page", "9");
            collection.Add("sort", "f536870924");
            collection.Add("order", "desc");

            var request = controller.Parse("form", collection,"condition");

            Assert.AreEqual(2, request.FieldIds.Count);
            Assert.AreEqual(10u, request.RetrieveCount);
            Assert.AreEqual(80u, request.StartIndex);
            Assert.AreEqual("condition", request.Qualification);
            Assert.AreEqual("form", request.FormName);
            Assert.AreEqual(536870924u, request.Sorts[0].FieldId);
            Assert.AreEqual(SortOrder.SORT_DESCENDING, request.Sorts[0].Order);
        }

        [TestMethod]
        public void JsonFromEntries_Test1()
        {
            //RawJsonResult
            TableController controller = new TableController();
            List<AREntry> entries = new List<AREntry>();
            AREntry entry = new AREntry();
            entry.FieldValues = new List<ARFieldValue>();
            entry.FieldValues.Add(new ARFieldValue { FieldId = 536870923 ,Value = 1,DataType = ARDataType.DATA_TYPE_INTEGER});
            entry.FieldValues.Add(new ARFieldValue { FieldId = 536870924, Value = "Mary", DataType = ARDataType.DATA_TYPE_CHAR });
            entries.Add(entry);

            AREntry entry2 = new AREntry();
            entry2.FieldValues = new List<ARFieldValue>();
            entry2.FieldValues.Add(new ARFieldValue { FieldId = 536870923, Value = 2, DataType = ARDataType.DATA_TYPE_INTEGER });
            entry2.FieldValues.Add(new ARFieldValue { FieldId = 536870924, Value = "Tom", DataType = ARDataType.DATA_TYPE_CHAR });
            entries.Add(entry2);

            JsonResult jr = controller.JsonFromEntries(entries.ToArray(), 10, JsonRequestBehavior.AllowGet);

            Assert.IsTrue(jr is RawJsonResult);
            Assert.AreEqual("application/json", jr.ContentType);
            Assert.AreEqual("{\"total\":10,\"rows\":[{\"f536870923\":1,\"f536870924\":\"Mary\"},{\"f536870923\":2,\"f536870924\":\"Tom\"}]}", jr.Data.ToString());

        }

    }
}
